package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.ApprovalData;
import com.lzx.iteam.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 04月 08日
 * Time: 下午3:42
 *
 * @QQ : 1234567890
 * **********************************
 */
public class ApprovalDataAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ApprovalData> mList = new ArrayList<ApprovalData>();
    private int mListType;

    public ApprovalDataAdapter(Context mContext,int listType) {
        this.mContext = mContext;
        this.mListType = listType;
    }

    public void bindData(ArrayList<ApprovalData> list){
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.app_fg_item_layout,null);
            holder = new ViewHolder();
            holder.apType = (ImageView) convertView.findViewById(R.id.ap_fg_item_iv);
            holder.apName = (TextView) convertView.findViewById(R.id.ap_tv_name);
            holder.apStatus = (TextView) convertView.findViewById(R.id.ap_tv_status);
            holder.apTime = (TextView) convertView.findViewById(R.id.ap_tv_time);
            holder.apWait = (ImageView) convertView.findViewById(R.id.ap_iv_wait);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        ApprovalData data = mList.get(position);
        int type = data.getApproval_type();
        switch (type){
            case 1:
                holder.apType.setImageResource(R.drawable.approval_travel_iv);
                if (mListType ==1){
                    holder.apName.setText("我的" + mContext.getString(R.string.approval_travel));
                }else{
                    holder.apName.setText(data.getUser_name() + "的" + mContext.getString(R.string.approval_travel));
                }
                break;
            case 2:
                holder.apType.setImageResource(R.drawable.approval_leave_iv);
                if (mListType ==1){
                    holder.apName.setText("我的" + mContext.getString(R.string.approval_leave));
                }else{
                    holder.apName.setText(data.getUser_name() + "的" + mContext.getString(R.string.approval_leave));
                }
                break;
            case 3:
                holder.apType.setImageResource(R.drawable.approval_out_iv);
                if (mListType ==1){
                    holder.apName.setText("我的" + mContext.getString(R.string.approval_out));
                }else{
                    holder.apName.setText(data.getUser_name() + "的" + mContext.getString(R.string.approval_out));
                }
                break;
            case 4:
                holder.apType.setImageResource(R.drawable.approval_sign_iv);
                if (mListType ==1){
                    holder.apName.setText("我的" + mContext.getString(R.string.approval_sign));
                }else{
                    holder.apName.setText(data.getUser_name() + "的" + mContext.getString(R.string.approval_sign));
                }
                break;
        }
        int status = data.getApproval_status();
        switch (status){
            case 0:
                holder.apStatus.setText("等待审批");
                break;
            case 1:
                holder.apStatus.setText("审批完成（同意）");
                break;
            case 2:
                holder.apStatus.setText("审批完成（拒绝）");
                break;
        }
        if (mListType == 1 && status == 0){
            holder.apWait.setVisibility(View.VISIBLE);
        }else{
            holder.apWait.setVisibility(View.GONE);
        }
        if (mListType == 2){
            holder.apStatus.setTextColor(mContext.getResources().getColor(R.color.attendance_calendar_red));
        }else{
            holder.apStatus.setTextColor(mContext.getResources().getColor(R.color.namecard_note));
        }
        long time = Long.parseLong(data.getApproval_time());
        if (time != 0){
            holder.apTime.setText(DateUtil.getTime(time * 1000));
        }

        return convertView;
    }

    class ViewHolder {
        ImageView apType,apWait;
        TextView apName,apStatus,apTime;
    }
}
